import { history } from 'umi';
import myPage from './modules/myPage';

const loginPath = '/user/login';

export default async function (): Promise<{
  settings?: any;
  currentUser?: API.CurrentUser;
  fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
  modules?: any; // to fix
}> {
  const fetchUserInfo: any = async () => {
    try {
      // const msg = await queryCurrentUser();
      // {
      //   data: {
      //     isLogin: false,
      //   },
      //   errorCode: '401',
      //   errorMessage: '请先登录！',
      //   success: true,
      // }
      return { isLogin: true };
    } catch (error) {
      history.push(loginPath);
    }
    return undefined;
  };
  // 如果是登录页面，不执行
  if (history.location.pathname !== loginPath) {
    const currentUser = await fetchUserInfo();
    return {
      fetchUserInfo,
      currentUser,
      settings: {},
      modules: {
        myPage,
      },
    };
  }
  return {
    fetchUserInfo,
    settings: {},
    modules: {
      myPage,
    },
  };
}
